package com.homestay.service.impl;

import com.homestay.beans.Books;
import com.homestay.beans.Order;
import com.homestay.dao.BooksDao;
import com.homestay.dao.impl.BooksDaoImpl;
import com.homestay.exception.UserException;
import com.homestay.service.BooksService;
import com.homestay.utils.JdbcUtils;

import java.util.Collections;
import java.util.List;

public class BooksServiceImpl implements BooksService {

    BooksDao booksDao = new BooksDaoImpl();
    @Override
    public int add(Books books) {
        int count1 = 0;
        int count2 = 0;
        try {
            JdbcUtils.startTransaction();
            count1 = JdbcUtils.executeUpdate("insert into tb_books values(null,?,?,?,null,now())", books.getVip_id(), books.getHome_id(), books.getEnterTime());
            count2 = JdbcUtils.executeUpdate("update tb_home set state = ? where id = ?", "已入住", String.valueOf(books.getHome_id()));
            JdbcUtils.commitTransaction();
            JdbcUtils.close();
        } catch (Exception e) {
            JdbcUtils.rollbackTransaction();
            throw new RuntimeException(e);
        }
        return count1 + count2;
    }

    @Override
    public List<Order> query(String name, String card, String phone) {
        return booksDao.query(name, card, phone);
    }

    @Override
    public int delete(String id) throws UserException {
        // 没有退房的记录不能删除
        Books books = booksDao.queryBookById(id);
        if (books.getExitTime() == null) {
            System.out.println("不能删");
            throw new UserException("当前顾客没有退房，记录不能删除");
        }
        return booksDao.delete(id);
    }

    @Override
    public Order queryById(String id) {
        return booksDao.queryById(id);
    }

    @Override
    public int update(Books books) {
        int count = 0;
        int count1 = 0;
        int count2 = 0;
        try {
            // 获取更新之前的房间的 id
            String homeId = booksDao.queryById(String.valueOf(books.getId())).getHomeId();
            // 关闭自动提交
            JdbcUtils.startTransaction();
            if(!homeId.equals(String.valueOf(books.getHome_id()))) {
                // 更新了房间
                // 更新以前的房间状态为未打扫
                count1 = JdbcUtils.executeUpdate("update tb_home set state = ? where id = ?","未打扫", homeId);
                // 更新现在房间状态为已入住
                count2 = JdbcUtils.executeUpdate("update tb_home set state = ? where id = ?","已入住", String.valueOf(books.getHome_id()));
            }
            // 更新数据库
            count = JdbcUtils.executeUpdate("update tb_books set vip_id = ?, home_id = ?, enterTime = ?, exitTime = ? where id = ?",
                        books.getVip_id(), books.getHome_id(), books.getEnterTime(), books.getExitTime(), books.getId());
            JdbcUtils.commitTransaction();
            JdbcUtils.close();
        } catch (Exception e) {
            JdbcUtils.rollbackTransaction();
            throw new RuntimeException(e);
        }
        return count + count1 + count2;
    }

    @Override
    public int updateExitTime(String id) {
        return booksDao.updateExitTime(id);
    }

    @Override
    public Books queryBookById(String id) {
        return booksDao.queryBookById(id);
    }
}
